package com.xyzh05.service.impl;

import java.util.Date;
import java.util.List;

import javax.jws.WebService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xyzh05.dao.PlanDao;
import com.xyzh05.dao.VipServiceDao;
import com.xyzh05.model.Plan;
import com.xyzh05.model.User;
import com.xyzh05.model.VipService;
import com.xyzh05.service.PlanManager;

@Service("planManager")
@WebService(serviceName = "PlanService", endpointInterface = "com.xyzh05.service.PlanManager")
public class PlanManagerImpl extends GenericManagerImpl<Plan, Long> implements
		PlanManager {
	PlanDao planDao;
	VipServiceDao vipServiceDao;

	@Autowired
	public PlanManagerImpl(PlanDao planDao, VipServiceDao vipServiceDao) {
		super(planDao);
		this.planDao = planDao;
		this.vipServiceDao = vipServiceDao;
	}

	public List<?> findQuery(Long vipServiceId) {
		return planDao.findQuery(" from Plan where vipService.id = ? order by type",	vipServiceId);
	}

	public void batchSave(List<Plan> plans, Long vipServiceId, User user) {		
		if (plans.size() > 0) {
			boolean falg = true;
			VipService vs =  vipServiceDao.get(vipServiceId);
			this.planDao.batchDel(" delete from Plan where vipService.id = ?  ", vipServiceId);
			for (Plan plan : plans) {
				plan.setUser(user);
				plan.setUpdateTime(new Date());
				plan.setVipService(vs);
				planDao.save(plan);
				if(plan.getStatus() != (short)2 && falg){
					falg = false;
				}
			}
			if(falg){
				vs.setStatus((short)1);
				vs.setUpdateTime(new Date());
				vipServiceDao.saveOrUpdate(vs);
			}			
		}
	}

	public Boolean existPlan(Long vipServiceId) {
		List<?> count = planDao.findQuery(" select count(p) from Plan p where p.vipService.id = ? ", vipServiceId);
		Long l = (Long)count.get(0);
		return l.equals(0L) ? true : false;
	}
}